<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="utf-8" />
		<meta name="renderer" content="webkit" />
		<meta name="force-rendering" content="webkit" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
		<meta
			name="viewport"
			content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"
		/>
		<title>查看信息</title>
		<style type="text/css">
			body {
				margin: auto;
			}

			#infoPage {
				padding: 0 14px 0 16px;
			}

			#infoTime {
				color: darkgray;
				margin: 8px 0 24px 0;
			}

			#infoText {
				color: dimgray;
				line-height: 175%;
			}
		</style>
		<script type="text/javascript" src="jweixin-1.3.2.js"></script>
		<script type="text/javascript">
			var basicUrl = 'https://ndx2345.top:446/api/';
			var domTitle;
			window.onload = init;

			function init() {
				var errMsg = '';
				domTitle = document.getElementById('infoTitle');
				if (!domTitle) errMsg = '未找到h1标签';
				if (errMsg) {
					document.body.textContent = errMsg;
					return;
				}

				var { value, err } = funcGetSearch('id');
				if (err) {
					domTitle.textContent = err;
					return;
				}

				var result = funcGetSearch('type', true);
				if (result.err) {
					domTitle.textContent = 'type不能为空';
					return;
				}

				var type = result.value;
				if (type !== 'content' && type !== 'notice') {
					domTitle.textContent = 'type的值不正确';
					return;
				}

				domTitle.textContent = '正在进行网络请求, 请稍候..';

				var params = { id: value };
				funcRequest('home/' + type + 'Detail', params, funcShow);
			}

			// 把拿到的数据呈现给用户
			function funcShow(data) {
				var title = data.model.title;
				document.title = title;
				domTitle.innerHTML = title;
				var dom = document.getElementById('infoTime');
				dom.textContent = '发布日期：' + data.model.addTime.substr(0, 10);
				dom = document.getElementById('infoText');
				dom.innerHTML = data.model.content;

				dom = document.getElementsByClassName('articleImage');
				if (!dom.length) return;

				var urls = [];
				for (var i = 0; i < dom.length; i += 1) {
					var img = dom[i];
					urls.push(img.src);
					img.onclick = function(e) {
						console.log(e.target.src);
						wx.previewImage({
							urls: urls,
							current: e.target.src,
						});
					};
				}
				console.log(urls);
			}

			// 网络请求
			function funcRequest(apiPath, params, callback) {
				fetch(basicUrl + apiPath, {
					method: 'post',
					body: JSON.stringify(params),
				})
					.then(response => response)
					.then(response => response.json())
					.then(response => {
						if (response.status === 1) {
							domTitle.textContent = response.msg;
							return;
						}

						if (callback) callback(response);
					});
			}

			// 获取地址栏里的某一个参数的值
			function funcGetSearch(paramsName, isString) {
				var search = location.search;
				var errMsg = '';
				var paramsValue = '';
				var pos = search.indexOf(paramsName);
				if (pos === -1) errMsg = '地址栏参数不能为空';
				if (!errMsg) {
					paramsValue = search.substr(pos + paramsName.length + 1);
					var pos2 = paramsValue.indexOf('&');
					if (pos2 > 0) paramsValue = paramsValue.substr(0, pos2);
					if (!errMsg && paramsValue === '') errMsg = '地址栏参数不能为空';
					if (!errMsg && !isString && isNaN(paramsValue)) errMsg = '地址栏参数的值必须是一个数字';
				}
				if (errMsg) {
					if (isString) return { value: '', err: errMsg };
					else return { value: 0, err: errMsg };
				} else {
					if (isString) return { value: paramsValue, err: errMsg };
					else return { value: paramsValue - 0, err: errMsg };
				}
			}
		</script>
	</head>
	<body>
		<div id="infoPage">
			<h3 id="infoTitle"></h3>
			<div id="infoTime"></div>
			<p id="infoText"></p>
		</div>
	</body>
</html>
